/*
  "name": "大山后台管理系统",
  "version": "1.0",
  "description": "大山中后台管理系统",
  "author": "大山",
  "date": "2023-04-04",
  "license": "MIT",
*/
const jwt = require("jsonwebtoken")

//撒盐，加密时候混淆
const secret = '123567890bingjspoiuytrewqlkjhgfdsa'

/*
*name: 生成token
*@param {}
*@param {string} 生成token字符串
*/
exports.createToken = function(info) {
	let token = jwt.sign(info, secret, {
		//Token有效时间 单位s，1小时
		expiresIn: 60 * 60 * 1
	})
	return token
}

/*
*name: 验证token
*@param {}
*@param {string} 验证token字符串
*/
exports.verifyToken = function(token) {
	return new Promise((resolve, reject) => {
	  jwt.verify(token, secret, (error, result) => {
		if(error){
			reject(error)
		} else {
			resolve(result)
		}
	  })
	})
}

/*
*name: token msg返回
*@param {}
*@param {array} 数组
*/
exports.tokenMsg = function() {
	let tokenMsg = [
		{type: 'jwt expired', msg: 'token已过期'},
		{type: 'jwt malformed', msg: '无效token'},
		{type: 'invalid token', msg: '无效token'},
		{type: 'invalid signature', msg: '无效token'},
		{type: 'jwt must be provided', msg: '必须提供token'},
	]
	return tokenMsg
}
